<?php
/**
 * @title 2. 两数相加 - 新建链表
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param ListNode $l1
     * @param ListNode $l2
     * @return ListNode
     */
    function addTwoNumbers($l1, $l2) {
        /**
         * @since 2023-12-25 链表$l1对应的数字元素Array
         */
        $num1Array = [];
        while($l1){
            $num1Array[] = $l1->val;
            $l1 = $l1->next;
        }

        /**
         * @since 2023-12-25 链表$l2对应的数字元素Array
         */
        $num2Array = [];
        while($l2){
            $num2Array[] = $l2->val;
            $l2 = $l2->next;
        }

        /**
         * @since 2023-12-25 数组对应长度
         */
        $num1Length = count($num1Array);
        $num2Length = count($num2Array);

        /**
         * @since 2023-12-25 2个数组长度保持一致，不足长度的补0
         */
        if($num1Length > $num2Length){
            $num2Array = array_merge($num2Array, array_fill(0, $num1Length-$num2Length, 0));
        } elseif($num1Length < $num2Length){
            $num1Array = array_merge($num1Array, array_fill(0, $num2Length-$num1Length, 0));
        } else {}

        /**
         * @since 2023-12-25 新建链表$l3
         */
        $l3 = new ListNode(null);

        /**
         * @since 2023-12-25 进位
         */
        $carry = 0;

        /**
         * @since 2023-12-25 数组1对应长度
         */
        $num1Length = count($num1Array);

        /**
         * @since 2023-12-25 2个数组元素相加
         */
        $addList = $l3;
        for($i=0; $i<$num1Length; $i++){
            $num = $num1Array[$i] + $num2Array[$i] + $carry;

            /**
             * @since 2023-12-25 进位
             */
            if($num>=10){
                $carry = 1;
                $num -= 10;
            } else {
                $carry = 0;
            }

            /**
             * @since 2023-12-25 更新链表值
             */
            $addList->val = $num;

            /**
             * @since 2023-12-25 继续迭代
             */
            if($i == $num1Length-1){
                /**
                 * @since 2023-12-25 最高位相加，如果有进位，需要新建一个链表节点
                 */
                if($carry) {
                    $addList->next = new ListNode($carry);
                } else {}
            } else {
                $addList->next = new ListNode(null);
                $addList = $addList->next;
            }
        }

        /**
         * @return
         */
        return $l3;
    }
}


/**
 * @url http://127.0.0.1/leetcode/202312/2023.12.24_3.php
 */
$datas = [
    // [[2,4,3], [5,6,4]], // 708
    // [[9,9,9,9,9,9,9], [9,9,9,9]], // 89990001
    // [[9], [9,9]], //
    [[3], [2]], //
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    if(count($data) <= 20){
        var_dump($data);
    } else {
        var_dump(count($data));
    }

    $obj = new Solution();
    // $result = $obj->(...$data);
    // $result = $obj->($xhprof->tree($data));
    $result = $obj->addTwoNumbers($xhprof->listNode($data[0]), $xhprof->listNode($data[1]));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
